local zhedai = fk.CreateSkill{
  name = "hanqing__zhedai",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["hanqing__zhedai"] = "蛰待",
  [":hanqing__zhedai"] = "锁定技，每回合结束时，若本回合：有牌因三种不同方式进入弃牌堆，你执行一个摸牌阶段；"..
  "有剩余出【杀】次数的，你执行一个出牌阶段；若均不满足，你执行一个弃牌阶段，",
}

zhedai:addEffect(fk.EventPhaseEnd, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(zhedai.name) and target.phase == Player.Finish then
      local choices = {}
      local types = {}
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            table.insertIfNeed(types, move.moveReason)
          end
        end
      end, Player.HistoryTurn)
      if #types > 2 then
        table.insert(choices, Player.Draw)
      end
      if player:getMark("hanqing__zhedai-turn") > 0 then
        table.insert(choices, Player.Play)
      end
      if #choices == 0 then
        table.insert(choices, Player.Discard)
      end
      choices = table.reverse(choices)
      event:setCostData(self, {choice = choices})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    for _, choice in ipairs(event:getCostData(self).choice) do
      player:gainAnExtraPhase(choice, zhedai.name, true)
    end
  end,

  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(zhedai.name, true) and target.phase == Player.Play then
      local card = Fk:cloneCard("slash")
      local skill = card.skill
      local n = skill:getMaxUseTime(target, Player.HistoryPhase, card, nil)
      if not n or target:usedCardTimes("slash", Player.HistoryPhase) < n then
        return true
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "hanqing__zhedai-turn", 1)
  end,
})

return zhedai
